package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckgroupCheckitemMapper;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckgroupCheckitem;
import com.itheima.pojo.SetmealCheckgroup;
import com.itheima.service.CheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;


@Service
public class CheckgroupServiceImpl implements CheckgroupService {

    @Autowired
    private CheckgroupMapper checkgroupMapper;


    @Autowired
    private CheckgroupCheckitemMapper cCMapper;

    @Autowired
    private SetmealCheckgroupMapper sCMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        LambdaQueryWrapper<CheckGroup> wrapper = new LambdaQueryWrapper<>();
        if (queryPageBean.getQueryString() != null) {
            wrapper.eq(CheckGroup::getCode, queryPageBean.getQueryString()).or().
                    like(CheckGroup::getName, queryPageBean.getQueryString()).or().
                    eq(CheckGroup::getHelpCode, queryPageBean.getQueryString());
        }
        IPage<CheckGroup> p = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        IPage<CheckGroup> checkGroupIPage = checkgroupMapper.selectPage(p, wrapper);
        return new PageResult(checkGroupIPage.getTotal(), checkGroupIPage.getRecords());
    }

    @Override
    public Result add(Integer[] checkitemIds, CheckGroup checkGroup) {
        //1、判断复选框 和信息表 是否有数据(哪个为空)
        //1.1、如果两个都为空 直接返回 参数异常，添加失败
        if (checkitemIds.length==0 && checkGroup == null) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //1.2、如果两个都不为空 同时添加数据表及复选框勾选中的数据到数据库
        if (checkitemIds.length!=0 && checkGroup != null) {
            //1.2.2、添加信息表数据到数据库
            checkgroupMapper.insert(checkGroup);
            //遍历itemIds
            for (Integer checkitemId : checkitemIds) {
                //1.2.1、添加复选框中的id到关联数据库
                cCMapper.insert(new CheckgroupCheckitem(checkGroup.getId(), checkitemId));
            }
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }
        //2、如果 复选框为空 信息表不为空
        if (checkitemIds.length==0 && checkGroup != null) {
            //2.1、添加信息表数据到数据库即可
            checkgroupMapper.insert(checkGroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }
        return new Result(false,  MessageConstant.ADD_CHECKGROUP_FAIL);
    }

    @Override
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkgroupMapper.selectById(id);
        return new Result(true, "查询检查组成功",checkGroup);
    }

    @Override
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        LambdaQueryWrapper<CheckgroupCheckitem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CheckgroupCheckitem::getCheckgroupId,id );
        List<CheckgroupCheckitem> checkgroupCheckitems = cCMapper.selectList(wrapper);
        //定义 checkItemIds
        Integer [] checkItemIds = new Integer[checkgroupCheckitems.size()];
        //遍历集合 取出 checkItem的id 放入 Ids数组中
        for (int i = 0; i < checkgroupCheckitems.size(); i++) {
            checkItemIds[i]=checkgroupCheckitems.get(i).getCheckitemId();
        }
        return new Result(true,"查询检查组成功",checkItemIds );
    }

    @Override
    public Result edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        //把关联表中checkGroup对应的checkitemIds 删除
        LambdaQueryWrapper<CheckgroupCheckitem> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(CheckgroupCheckitem::getCheckgroupId, checkGroup.getId());
        cCMapper.delete(deleteWrapper);
        //把修改后的checkGroup数据装入数据库
        checkgroupMapper.updateById(checkGroup);
        //把修改后的checkItemId 放入数据库
        for (Integer checkitemId : checkitemIds) {
            //1.2.1、添加复选框中的id到关联数据库
            cCMapper.insert(new CheckgroupCheckitem(checkGroup.getId(), checkitemId));
        }
        return new Result(true, "编辑检查组成功",null);
    }

    @Override
    public Result findAll() {
        List<CheckGroup> checkGroups = checkgroupMapper.selectList(null);
        return new Result(true,"查询成功",checkGroups );
    }

    @Override
    public Result findCheckGroupIdsBySetmealId(Integer id) {
        LambdaQueryWrapper<SetmealCheckgroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealCheckgroup::getSetmealId, id);
        List<SetmealCheckgroup> setmealCheckgroupList = sCMapper.selectList(wrapper);
        Integer[] checkGroupIds = new Integer[setmealCheckgroupList.size()];
        for (int i = 0; i < setmealCheckgroupList.size(); i++) {
            checkGroupIds[i]=setmealCheckgroupList.get(i).getCheckgroupId();
        }
        return new Result(true,"查询检查组成功",checkGroupIds);
    }


}
